import hashlib
from django.shortcuts import redirect
from functools import wraps


def sha_pwd(pwd):
    """
    shal 加密密码
    :param pwd:
    :return:
    """
    m = hashlib.sha1("admin".encode("utf-8"))
    m.update(pwd.encode("utf-8"))
    return m.hexdigest()


def is_login(func):
    """
    判断用户是否有登录的装饰器
    :param func:
    :return:
    """

    @wraps(func)  # 这个装饰器是为了保存被装饰的原有属性
    def inner(request, *args, **kwargs):
        username = request.session.get("username")
        openid = request.session.get("openid")
        if username or openid:
            return func(request, *args, **kwargs)
        else:
            return redirect("/login/")

    return inner